//
//  TableViewCellOfInternet.m
//  UI_Project_A
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "TableViewCellOfInternet.h"
#import <UIImageView+WebCache.h>
#import "ModelOfInternet.h"
#import <DKNightVersion.h>

#define WIDTH self.frame.size.width
#define HEIGHT  self.frame.size.height


@interface TableViewCellOfInternet ()

@property (nonatomic ,retain)UILabel *label;
@property (nonatomic ,retain)UIImageView *imageviewInt;
@property (nonatomic, retain) ModelOfInternet *model;
@property (nonatomic ,retain)UILabel *labelTime;
@end


@implementation TableViewCellOfInternet

- (void)dealloc {
    [_label release];
    [_imageviewInt release];
    [_model release];
    [_labelTime release];
    [super dealloc];
}






- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self createLabel];
       [self createImageview];
    }
    
    return self;
}



- (void)passModelOfData:(ModelOfInternet *)model {


    self.model = model;
    
    self.label.text = model.title;
    NSString *url = [self Substring:model.url];
 
    
    [self.imageviewInt sd_setImageWithURL:[NSURL URLWithString:url]];
    
    
    
    
    //timeLabel
    //时间
    
    NSString *modelTime = [model.ts substringToIndex:10];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"HH:mm"];
     [dateFormatter setDateFormat:@"MM-dd"];
       [dateFormatter release];


    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (int)time - modelTime.doubleValue;
       float points = seconds / 60;
    if (points < 60) {
        self.labelTime.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
    }else if (points > 60 && points < 3600) {
        
        self.labelTime.text = [NSString stringWithFormat:@"1小时前"];
    }else {
        self.labelTime.text = [NSString stringWithFormat:@"2小时前"];
    }
    
    
    
   
}

- (NSString *)Substring:(NSString *)urlString {

    NSString *sub = urlString;
    
    if ([sub hasSuffix:@"jpg"]) {
        return urlString;
    }else {
        NSRange range = [urlString rangeOfString:@"u="];
        NSString *sub1 = [urlString substringFromIndex:range.location+2];
        
        NSRange lastRange = [sub1 rangeOfString:@"&"];
        sub = [sub1 substringToIndex:lastRange.location];
        
    }


    
    return sub;
}


#pragma mark - label
- (void)createLabel {
    
    self.label = [[UILabel alloc]init];
     self.label.numberOfLines = 0;
    [self addSubview:self.label];
    [self.label release];
    
    
    
    
    // Timelabel
    
    self.labelTime = [[UILabel alloc] init];
    self.labelTime.numberOfLines = 0;
    [self addSubview:self.labelTime];
    [self.labelTime release];

    
    self.label.dk_textColorPicker = DKColorWithColors([UIColor colorWithRed:0.118 green:0.125 blue:0.157 alpha:1.000], [UIColor whiteColor]);
    
    self.labelTime.dk_textColorPicker =  DKColorWithColors([UIColor colorWithRed:0.118 green:0.125 blue:0.157 alpha:1.000], [UIColor whiteColor]);
}


- (void)createImageview {
    self.imageviewInt = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
  
    [self addSubview:self.imageviewInt];
    [self.imageviewInt release];
    
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    if (!self.model.url) {
        self.label.frame = CGRectMake(20, 10,WIDTH - 25, self.frame.size.height/2);
    } else {
        self.label.frame = CGRectMake(20, 10, WIDTH/3 * 2 - 25, self.frame.size.height/2);
    }
    
    self.imageviewInt.frame = CGRectMake(WIDTH * 2/3, 10, WIDTH/3-10, HEIGHT - 10);
    self.labelTime.frame = CGRectMake(20, HEIGHT/5 * 3.5, 100, 30);

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{





}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
